
local meta = FindMetaTable( "Player" );

function meta:SayLocalOOCChat( text )

	local tbl = ents.FindInSphere( self:GetPos(), 180 );
	
	local rec = RecipientFilter();
	
	for k, v in pairs( tbl ) do
	
		if( v:IsPlayer() ) then
		
			rec:AddPlayer( v );
			v:PrintMessage( 2, self:GetRPName() .. ": " .. text );
		
		end
	
	end

	umsg.Start( "LocalOOCChat", rec );
		umsg.String( self:GetRPName() );
		umsg.String( text );
	umsg.End();	

end

function meta:Dispatch( text )

	if( not self:Alive() or not self:GetPlayerConscious() ) then
		return;
	end

	local rec = RecipientFilter();
	
	for k, v in pairs( player.GetAll() ) do
	
		if( v:IsPlayer() ) then
	
			rec:AddPlayer( v );
			v:PrintMessage( 2, self:GetRPName() .. "[DISPATCH] " .. text );
			
		end	
		
	end

	umsg.Start( "msgDispatch", rec );
		umsg.String( "(DISPATCH) " .. text );
	umsg.End();	
	
end	

function meta:Broadcast( text )

	if( not self:Alive() or not self:GetPlayerConscious() ) then
		return;
	end

	local rec = RecipientFilter();
	
	for k, v in pairs( player.GetAll() ) do
	
		if( v:IsPlayer() ) then
	
			rec:AddPlayer( v );
			v:PrintMessage( 2, self:GetRPName() .. "[BROADCAST] " .. text );
			
		end	
		
	end

	umsg.Start( "msgBroadcast", rec );
		umsg.String( self:GetRPName() );
		umsg.String( "::[BROADCAST]:: " .. text );
	umsg.End();	
	
end	

function meta:VortLocalChat( text )

	if( not self:Alive() or not self:GetPlayerConscious() ) then
		return;
	end

	local tbl = ents.FindInSphere( self:GetPos(), 180 );
	
	local rec = RecipientFilter();
	
	for k, v in pairs( tbl ) do
	
		if( v:IsPlayer() and v:IsVort() ) then
		
			rec:AddPlayer( v );
			v:PrintMessage( 2, self:GetRPName() .. ": [Vortigese] " .. text );
		
		end
		
		if( v:IsItem() ) then
		
			if( v:GetTable().ItemData.PlayerChat ) then
				
				v:GetTable().ItemData.PlayerChat( v:GetTable().ItemData, self, v, text, ( v:GetPos() - self:EyePos() ):Length(), 1 );
				
			end

		end
	
	end

	umsg.Start( "ChVortigese", rec );
		umsg.String( self:GetRPName() );
		umsg.String( text );
	umsg.End();

end

function meta:SayICAction( text )

	if( not self:Alive() or not self:GetPlayerConscious() ) then
		return;
	end

	if( string.len( text ) < 1 ) then return; end
	if( string.gsub( text, " ", "" ) == "" ) then return; end

	local tbl = ents.FindInSphere( self:GetPos(), 180 );
	
	local rec = RecipientFilter();
	
	for k, v in pairs( tbl ) do
	
		if( v:IsPlayer() ) then
		
			rec:AddPlayer( v );
			v:PrintMessage( 2, "(" .. self:GetRPName() .. ") " .. text );
		
		end
	
	end

	umsg.Start( "SayICAction", rec );
		umsg.String( string.sub( text, 2 ) );
	umsg.End();	
	

end

function meta:DoICAction( text )

	if( not self:Alive() or not self:GetPlayerConscious() ) then
		return;
	end

	if( string.len( text ) < 1 ) then return; end
	if( string.gsub( text, " ", "" ) == "" ) then return; end

	local tbl = ents.FindInSphere( self:GetPos(), 180 );
	
	local rec = RecipientFilter();
	
	for k, v in pairs( tbl ) do
	
		if( v:IsPlayer() ) then
		
			rec:AddPlayer( v );
			v:PrintMessage( 2, self:GetRPName() .. text );
		
		end
	
	end

	umsg.Start( "ICAction", rec );
		umsg.String( "** " .. self:GetRPName() );
		umsg.String( text );
	umsg.End();	

end

function meta:WhisperLocalChat( text )

	if( not self:Alive() or not self:GetPlayerConscious() ) then
		return;
	end

	local tbl = ents.FindInSphere( self:GetPos(), 90 );
	
	local rec = RecipientFilter();
	
	for k, v in pairs( tbl ) do
	
		if( v:IsPlayer() ) then
		
			rec:AddPlayer( v );
			v:PrintMessage( 2, self:GetRPName() .. ": [WHISPER] " .. text );
		
		end
		
		if( v:IsItem() ) then
		
			if( v:GetTable().ItemData.PlayerChat ) then
				
				v:GetTable().ItemData.PlayerChat( v:GetTable().ItemData, self, v, text, ( v:GetPos() - self:EyePos() ):Length(), 1 );
				
			end

		end
	
	end

	umsg.Start( "WhisperICChat", rec );
		umsg.String( self:GetRPName() );
		umsg.String( text );
	umsg.End();

end


function meta:YellLocalChat( text )

	if( not self:Alive() or not self:GetPlayerConscious() ) then
		return;
	end

	local tbl = ents.FindInSphere( self:GetPos(), 600 );
	
	local rec = RecipientFilter();
	
	for k, v in pairs( tbl ) do
	
		if( v:IsPlayer() ) then
		
			rec:AddPlayer( v );
			v:PrintMessage( 2, self:GetRPName() .. ": [YELL] " .. text );
		
		end
	
		if( v:IsItem() ) then
		
			if( v:GetTable().ItemData.PlayerChat ) then
				
				v:GetTable().ItemData.PlayerChat( v:GetTable().ItemData, self, v, text, ( v:GetPos() - self:EyePos() ):Length(), 3 );
				
			end

		end
	
	end

	umsg.Start( "YellICChat", rec );
		umsg.String( self:GetRPName() );
		umsg.String( text );
	umsg.End();

end

function meta:SayLocalChat( text )

	if( not self:Alive() or not self:GetPlayerConscious() ) then
		return;
	end

	local tbl = ents.FindInSphere( self:GetPos(), 180 );
	
	local rec = RecipientFilter();
	
	for k, v in pairs( tbl ) do
	
		if( v:IsPlayer() ) then
		
			rec:AddPlayer( v );
			v:PrintMessage( 2, self:GetRPName() .. " " .. text );
		
		end
		
		if( v:IsItem() ) then
		
			if( v:GetTable().ItemData.PlayerChat ) then
				
				v:GetTable().ItemData.PlayerChat( v:GetTable().ItemData, self, v, text, ( v:GetPos() - self:EyePos() ):Length(), 2 );
				
			end
		
		end
	
	end

	umsg.Start( "ICChat", rec );
		umsg.String( self:GetRPName() );
		umsg.String( text );
	umsg.End();

end

function meta:SayGlobalChat( text )

	umsg.Start( "OOCChat" );
		umsg.String( self:GetRPName() );
		umsg.Short( self:Team() );
		umsg.String( text );
	umsg.End();
	
	return "[OOC] " .. text;

end

if( not meta.OrigPrintMessage ) then
	meta.OrigPrintMessage = meta.PrintMessage;
end

function meta:PrintMessage( type, msg )

	if( type == 2 ) then
	
		meta.OrigPrintMessage( self, type, msg );
	
	else

		umsg.Start( "PrintBlueMessage", self );
			umsg.String( msg );
		umsg.End();
		
	end

end

function meta:SendToStationRadio( text )

	if( self:HasItem( "radio" ) ) then

		for k, v in pairs( ents.FindByClass( "ts2_radio" ) ) do
	
			if( v:GetNWInt( "radiof" ) == self:GetPlayerRadioFreq() ) then
	
				umsg.Start( "SendRadioPiece");
					umsg.String( text );
					umsg.Float( v:EntIndex() );
				umsg.End()
		
			end	
		
		end	
		
	end	
	
end	

function meta:RadioTalk( text, notice )

	local rec = RecipientFilter();
	
	if( self:HasItem( "radio" ) ) then
	
		for _, v in pairs( player.GetAll() ) do
		
			if( v:GetPlayerRadioFreq() == self:GetPlayerRadioFreq() ) then
			
				rec:AddPlayer( v );
				v:PrintMessage( 2, text );
				
			end	
			
		end

    end

	umsg.Start( "AddChatLine", rec );
		umsg.String( text );
		umsg.Bool( notice );
	umsg.End();
	
end	

function meta:TalkToCPCR( text )

	local rec = RecipientFilter();

	if( self:HasItem( "crradio" ) ) then
	
		for _, v in pairs( player.GetAll() ) do
		
			if( v:IsCombine() ) then
			
			rec:AddPlayer( v );
				v:PrintMessage( 2, text );
				
			end	
			
		end

    end
			
	umsg.Start( "AddChatLine", rec );
		umsg.String( text );
		umsg.Bool( notice );
	umsg.End();
	
end				
	
	